/**
 * Copyright (C) 2013 Oleg Anastassov <oleg.anastassov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.loc.util;

import java.awt.*;

/**
 * Date: 01.01.2013
 * Time: 18:04
 *
 * @author Oleg Anastassov
 */
public final class GUIUtils {

    private static Rectangle maxWindowBounds;

    private GUIUtils() {
    }

    /**
     * Максимально допустимые границы экрана.
     *
     * @return размеры экрана монитора
     */
    public static Rectangle monitorBounds() {
        if (maxWindowBounds != null) {
            return maxWindowBounds;
        }
        maxWindowBounds = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getMaximumWindowBounds();
        return maxWindowBounds;
    }

    /**
     * Расположение окна в центре экрана
     *
     * @param aWindow показываемое окно
     */
    public static void centerAndShow(Window aWindow) {
        centerAndShow(aWindow, null);
    }

    public static void centerAndShow(Window aWindow, Window parent) {
        Rectangle screen = parent != null ? parent.getBounds() : monitorBounds();
        Dimension window = aWindow.getSize();

        if (window.height > screen.height) {
            window.height = screen.height;
        }
        if (window.width > screen.width) {
            window.width = screen.width;
        }

        int xCoord = screen.x + (screen.width / 2 - window.width / 2);
        int yCoord = screen.y + (screen.height / 2 - window.height / 2);

        aWindow.setLocation(xCoord, yCoord);
        aWindow.setVisible(true);
    }

}
